//
//  QRCodeResultViewController.m
//  GolfFriend
//
//  Created by Vescky on 14-5-16.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "QRCodeResultViewController.h"
#import "AppsEngine.h"
#import <QuartzCore/QuartzCore.h>
#import "UserSession.h"

#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
#import "UIImageView+MJWebCache.h"

#import "ShareManager.h"
#import "SVProgressHUD.h"

@interface QRCodeResultViewController ()

@end

@implementation QRCodeResultViewController

@synthesize sourceLink;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"十二连拍";
    [self initData];
    [super customBackButton];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initData {
    labelUserName.text = [[UserSession shareUserSession] getUserName];
    labelMotto.text = [[[UserSession shareUserSession] getUserPublicInfo] objectForKey:@"motto"];
    imgvAvatar.layer.cornerRadius = imgvAvatar.frame.size.width / 2.0;
    NSString *avatarLink = [[[UserSession shareUserSession] getUserPublicInfo] objectForKey:@"avatar"];
    if ([avatarLink stringByMatching:Qiniu_Domain]) {
        avatarLink = GetQiniuResizeImageLink(avatarLink, 160);
    }
    [imgvAvatar setImageURLStr:avatarLink placeholder:Default_Avatar];
    imgvAvatar.layer.cornerRadius = imgvAvatar.frame.size.width / 2.0;
    
    NSString *contentLink = sourceLink;
    if ([contentLink stringByMatching:Qiniu_Domain]) {
        contentLink  = GetQiniuResizeImageLink(contentLink, 300);
    }
    [imgvContent setImageURLStr:contentLink placeholder:Default_Image_Normal];
    [imgvContent addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)]];
}

- (void)tapImage:(UITapGestureRecognizer *)tap
{
    // 1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:1];
    MJPhoto *photo = [[MJPhoto alloc] init];
    photo.url = [NSURL URLWithString:sourceLink]; // 图片路径
    photo.srcImageView = imgvContent; // 来源于哪个UIImageView
    [photos addObject:photo];
    
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = 0; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    [browser show];
}


- (IBAction)btnAction:(id)sender {
    
    UIButton *btn = (UIButton*)sender;
    if (btn.tag == 101) {
        //下载
        NSLog(@"下载");
        UIImageWriteToSavedPhotosAlbum(imgvContent.image, nil, nil, nil);
        [SVProgressHUD showSuccessWithStatus:@"图片已存到相册!"];
    }
    else if (btn.tag == 102) {
        //分享
        NSString *shareLink = [NSString stringWithFormat:@"http://mingyuanhui.sinaapp.com/index.php/webview/index/photo12?filename=%@",[sourceLink lastPathComponent]];
        NSLog(@"分享,%@",shareLink);
        [[ShareManager shareManeger] shareToSocialPlatformWithUrl:shareLink title:@"名媛荟分享" text:@"分享我的一张图片!" image:imgvContent.image parrentViewController:self];
    }
}


@end
